/*************************
Diese Software ist ein Beispiel (sample code) und unterliegt der Microsoft Public License. 
Die Verwendung des Codes ist unter den Bedingungen der Microsoft Public License erlaubt.
*************************
This software is sample code and is subject to the Microsoft Public License. 
You may use this code according to the conditions of the Microsoft Public License.
*************************/

using System;
using System.Collections.Specialized;
using System.Security.Principal;
using System.Collections.Generic;
using Egora.Pvp;

namespace Egora.PvpHttpModule
{
  public class PvpIdentity : IPvpIdentity
  {
    private static readonly string s_userIdKey = PvpHeaderNames.GetHeaderName(PvpHeader.X_AUTHENTICATE_UserID);
    private static readonly string s_gvOuIdKey = PvpHeaderNames.GetHeaderName(PvpHeader.X_AUTHENTICATE_gvOuId);
    private static readonly string s_ouKey = PvpHeaderNames.GetHeaderName(PvpHeader.X_AUTHENTICATE_Ou);
    private static readonly string s_versionKey = PvpHeaderNames.GetHeaderName(PvpHeader.X_Version);
    private static readonly string s_participantIdKey = PvpHeaderNames.GetHeaderName(PvpHeader.X_AUTHENTICATE_participantId);
    private static readonly string s_cnKey = PvpHeaderNames.GetHeaderName(PvpHeader.X_AUTHENTICATE_cn);
    private static readonly string s_secClassKey = PvpHeaderNames.GetHeaderName(PvpHeader.X_AUTHENTICATE_gvSecClass);
    private static readonly string s_mailKey = PvpHeaderNames.GetHeaderName(PvpHeader.X_AUTHENTICATE_mail);
    private static readonly string s_phoneKey = PvpHeaderNames.GetHeaderName(PvpHeader.X_AUTHENTICATE_tel);
    private static readonly string s_gvGidKey = PvpHeaderNames.GetHeaderName(PvpHeader.X_AUTHENTICATE_gvGid);
    private static readonly string s_gvFunctionKey = PvpHeaderNames.GetHeaderName(PvpHeader.X_AUTHENTICATE_gvFunction);
    private static readonly string s_bpkKey = PvpHeaderNames.GetHeaderName(PvpHeader.X_AUTHENTICATE_bpk);

    private NameValueCollection _headers = new NameValueCollection();
    private bool _isAuthenticated = false;

    public PvpIdentity(NameValueCollection headers)
    {
      _headers = headers;
      _isAuthenticated = (_headers[s_userIdKey] != null) 
                      && (_headers[s_versionKey] != null)
                      && (_headers[s_gvOuIdKey] != null)
                      && (_headers[s_ouKey] != null);
    }

    /// <summary>
    ///                     Gets the name of the current user.
    /// </summary>
    /// <returns>
    ///                     The name of the user on whose behalf the code is running.
    /// </returns>
    public virtual string Name
    {
      get { return _headers[s_userIdKey]; }
    }

    /// <summary>
    ///                     Gets the type of authentication used.
    /// </summary>
    /// <returns>
    ///                     The type of authentication used to identify the user.
    /// </returns>
    public virtual string AuthenticationType
    {
      get { return (_headers[s_versionKey]==null) ? null : "PVP Version " + _headers[s_versionKey]; }
    }

    /// <summary>
    ///                     Gets a value that indicates whether the user has been authenticated.
    /// </summary>
    /// <returns>
    /// true if the user was authenticated; otherwise, false.
    /// </returns>
    public virtual bool IsAuthenticated
    {
      get { return _isAuthenticated; }
    }

    public virtual string GetHeader(string key)
    {
      return _headers[key];
    }

    public virtual string[] GetHeaders(string key)
    {
      return _headers.GetValues(key);
    }

    public virtual string GetHeader(AuthenticatePvpHeader header)
    {
      return _headers[PvpHeaderNames.GetHeaderName(header)];
    }

    public virtual string[] GetHeaders(AuthenticatePvpHeader header)
    {
      return _headers.GetValues(PvpHeaderNames.GetHeaderName(header));
    }

    #region Implementation of IPvpIdentity

    public string ParticipantId
    {
      get { return GetHeader(s_participantIdKey); }
      set { throw new System.NotImplementedException(); }
    }

    public string UserId
    {
      get { return GetHeader(s_userIdKey); }
      set { throw new System.NotImplementedException(); }
    }

    public string Cn
    {
      get { return GetHeader(s_cnKey); }
      set { throw new System.NotImplementedException(); }
    }

    public string GvOuId
    {
      get { return GetHeader(s_gvOuIdKey); }
      set { throw new System.NotImplementedException(); }
    }

    public string Ou
    {
      get { return GetHeader(s_ouKey); }
      set { throw new System.NotImplementedException(); }
    }

    public int SecClass
    {
      get { return Int32.Parse(GetHeader(s_secClassKey)); }
      set { throw new System.NotImplementedException(); }
    }

    public string Mail
    {
      get { return GetHeader(s_mailKey); }
      set { throw new System.NotImplementedException(); }
    }

    public string Phone
    {
      get { return GetHeader(s_phoneKey); }
      set { throw new System.NotImplementedException(); }
    }

    public string GvGid
    {
      get { return GetHeader(s_gvGidKey); }
      set { throw new System.NotImplementedException(); }
    }

    public string GvFunction
    {
      get { return GetHeader(s_gvFunctionKey); }
      set { throw new System.NotImplementedException(); }
    }

    public string[] Bpk
    {
      get { return GetHeaders(s_bpkKey); }
      set { throw new System.NotImplementedException(); }
    }

    #endregion
  }
}